home *** CD-ROM | disk | FTP | other *** search
- Path: rational.com!usenet
- From: Jerome Desquilbet <jDesquilbet@Rational.COM>
- Newsgroups: comp.lang.ada,comp.lang.c++
- Subject: Re: on OO differnces between Ada95 and C++
- Date: Fri, 23 Feb 1996 10:55:03 +0100
- Organization: Rational Software Corporation, France
- Message-ID: <312D8EF7.167E@Rational.COM>
- References: <4gbq7q$g08@qualcomm.com> <3129F185.41C6@Rational.COM> <4gi413$qo1@druid.borland.com>
- NNTP-Posting-Host: pigalle.rational.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (X11; I; OSF1 V3.2 alpha)
-
- ...YET ANOTHER RELIGIOUS WAR...
-
- Pete Becker wrote:
-
- > >#define private public // *** BERK! ***
- > >#include "...h" // second definition for the same class
- > >#undef private
- >
- > This is not true. A program that attempts to do this violates the one
- > definition rule, so it is not a legal C++ program.
-
- Pete,
-
- What is this "one definition rule"? Could you point me to the
- appropriate C++ Draft Standard location?
-
- Try this:
-
- // shape.h -----------------------------------------------------------
-
- #ifndef SHAPE_H_
- #define SHAPE_H_
- class Shape {
- public:
- Shape();
- virtual void Draw () const = 0;
- virtual void Erase () const = 0;
- void Move (int Delta_X, int Delta_Y);
- void Rotate(int Delta_A);
- private:
- int X, Y, A;
- };
- #endif
-
- // shape.C -----------------------------------------------------------
-
- #include "shape.h"
- #include <iostream.h>
-
- Shape::Shape() {X=Y=A=0;}
-
- void Shape::Move(int Delta_X, int Delta_Y) {
- cout << "I am a shape and I begin to move from ("
- << X << ',' << Y << ").\n";
- Erase(); X+=Delta_X; Y+=Delta_Y; Draw();
- cout << "I am a shape and I have finished moving to ("
- << X <<',' << Y << ").\n";
- }
- void Shape::Rotate(int Delta_A) {
- cout << "I am a shape and I begin to rotate from " << A << ".\n";
- Erase(); A+=Delta_A; Draw();
- cout << "I am a shape and I have finished rotating to " << A << ".\n";
- }
-
- // rectangle.h --------------------------------------------------------
-
- #ifndef RECTANGLE_H_
- #define RECTANGLE_H_
- #include "shape.h"
- class Rectangle : public Shape {
- public:
- Rectangle();
- void Draw () const;
- void Erase () const;
- void Resize(int Delta_H, int Delta_W);
- private:
- int H, W;
- };
- #endif
-
- // rectangle.C --------------------------------------------------------
-
- #include "rectangle.h"
- #include <iostream.h>
-
- Rectangle::Rectangle() : Shape() {H=W=0;}
-
- void Rectangle::Draw() const {
- cout << "I am a rectangle and I draw myself.\n";
- }
- void Rectangle::Erase() const {
- cout << "I am a rectangle and I erase myself.\n";
- }
- void Rectangle::Resize(int Delta_H, int Delta_W) {
- cout << "I am a rectangle and I begin to change my size.\n";
- Erase(); H+=Delta_H; W+=Delta_W; Draw();
- cout << "I am a rectangle and I have finished changing my size.\n";
- }
-
- // try_shape_rectangle_dirty_way.C ------------------------------------
-
- #define private public // *** BERK! ***
- #include "rectangle.h" // second definition for class Rectangle
- #undef private // in the program
- int main() {
- Rectangle R;
- Shape* pS;
- pS = &R;
- pS->X = 8;
- pS->Y = 18;
- pS->Move(10, 20);
- }
-
-
- It gives the following log:
-
-
- I am a shape and I begin to move from (8,18).
- I am a rectangle and I erase myself.
- I am a rectangle and I draw myself.
- I am a shape and I have finished moving to (18,38).
-
-
- Note that the initial coordinates were (8,18).
-
- The whole program contains two definitions for the same classes Shape
- and Rectangle: for each, one definition with attributes private and
- another definition with everything public.
-
- It seems that in C++, every encapsulation can be legally broken,
- essentially because of _independent_ compilation that allows
- redefinitions of the same class.
-
- Ada compilation model is different: it's _separate_ compilation.
- Impossible in Ada to have a redefinition in the same program: it's
- detected at compile-time. If something is checked in C++, it's deferred
- to link-time (for example a redefinition of the same class that will
- clash because the two constructors will have the same name).
-
-
- Read also "The design and evolution of C++" by Bjarne Stroustrup,
- chapter 17 "Namespaces", section 3 "Ideals for a solution": Ada has a
- more complete solution than C++.
-
- The basic property of OO languages is encapsulation. According to this
- criteria, C++ fails.
-
- Non mais enfin, quoi !
-
-
- Jerome.
-